package com.hui.servlet;


import com.hui.entity.Manager;
import com.hui.entity.Menu;
import com.hui.service.ManagerService;
import com.hui.service.MenuService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;


/**
 * @Description 管理员控制层
 * @date 2023/2/11 11:15
 */
@WebServlet(urlPatterns = {"/login", "/toIndex", "/logout"})
public class LoginServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri = req.getRequestURI();
        if (uri.endsWith("login")) {
            login(req, resp);
        } else if (uri.endsWith("toIndex")) {
            toIndex(req, resp);
        } else if (uri.endsWith("logout")) {
            logout(req, resp);
        }
    }

    /**
     * 退出登录
     **/
    private void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.invalidate();//销毁session

        resp.sendRedirect("login");
    }

    /**
     * 跳转主页，发送菜单数据
     **/
    private void toIndex(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session = req.getSession();
        Manager manager = (Manager) session.getAttribute("currentUser");

        MenuService service = new MenuService();
        List<Menu> list = service.findMenuById(manager.getMgrId());

        req.setAttribute("list", list);
//        System.out.println(list);
        req.setAttribute("manager", manager);

        req.getRequestDispatcher("index.jsp").forward(req, resp);

    }

    /**
     * 登录方法
     **/
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req. setCharacterEncoding ("utf-8");
        String loginName = req.getParameter("loginName");
        String loginPwd = req.getParameter("loginPwd");
        String code = req.getParameter("code");

        HttpSession session = req.getSession();

        String gCode = (String) session.getAttribute("code");

        if (gCode == null) {
            req.getRequestDispatcher("login.jsp").forward(req, resp);
            return;
        }

        if (!code.equals(gCode)) {
            req.setAttribute("msg", "验证码错误");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
            return;
        }

        ManagerService service = new ManagerService();
        Manager manager = service.login(loginName, loginPwd);

        if (manager != null) {
            //把当前用户对象，放入session中
            session.setAttribute("currentUser", manager);

            resp.sendRedirect("toIndex");
        } else {
            req.setAttribute("msg", "登录失败,请检查用户名和密码后重试");
            req.getRequestDispatcher("login.jsp").forward(req, resp);
        }

    }
}
